package net.zoneland.knowledge.service.impl.search;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zoneland.knowledge.model.EsQuestion;
import net.zoneland.knowledge.service.EsService;

/**
 * 根据题库一级科目或者二级科目精确匹配查询.
 *
 * @author: bcc
 * 2021/5/26
 */
@Service("searchOperationImplQueryExact")
public class SearchOperationImplQueryExact extends AbstractSearchOperation {

    /**
     * logger.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(SearchOperationImplQueryExact.class);

    @Autowired
    private EsService esService;

    @Override
    public boolean needRun(final SearchDto searchDto) {
        return !searchDto.isStopSearch() && StringUtils.isNotEmpty(searchDto.getQaQueryDto().getQueryText());
    }

    @Override
    protected void doSearchOrDeal(final SearchDto searchDto) throws Exception {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("start queryQuestionExact {}", searchDto);
        }
        try {
            List<EsQuestion> result = esService.queryExact(searchDto.getQaQueryDto());
            if (CollectionUtils.isNotEmpty(result)) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(" queryQuestionExact size {}", result.size());
                }
                searchDto.addEsQuestions(result);
                searchDto.setStopSearch(needStop(searchDto));
            }
        } catch (Exception e) {
            searchDto.setErrorFlag(true);
            LOGGER.error("SearchOperationImplQueryExact error", e);
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("end queryQuestionExact {}", searchDto);
        }
    }



}
